package giit.computerbase.util;

import net.sf.json.JSONObject;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;

/**
 * User: tum0r
 * Package Name: giit.computerbase.util
 * Time: 2018/11/10 Saturday 22:55:33
 */
public class Config {

    public HashMap<String, Object> pageConfig;

    public HashMap<String, Object> databaseConfig;

    public Config() {
        pageConfig = new HashMap<>();
        databaseConfig = new HashMap<>();
        pageConfig = parseJSON("../../config/PageConfig.json");
        databaseConfig = parseJSON("../../config/DatabaseConfig.json");
    }

    public HashMap<String, Object> parseJSON(String path) {
        HashMap<String, Object> result = new HashMap<>();
        try {
            File file = new File(Application.PATH + path);
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            StringBuffer stringBuffer = new StringBuffer();
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
            }
            JSONObject jsonObject = JSONObject.fromObject(stringBuffer.toString());
            Iterator<String> iterator = jsonObject.keys();
            while (iterator.hasNext()) {
                line = iterator.next();
                result.put(line, jsonObject.get(line));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

}
